#import "TransferInController.h"
//#import "ScanModel.h"
#import "TDTitleBtn.h"
#import "ChoseCoinController.h"
//#import "WalletModel.h"
@interface TransferInController ()
{
    //    UIButton *btn;
    UIImageView *icon;
    UILabel *titleLab;
    UIButton *saveBtn;
    UILabel *textLab;
    NSString *zc_min;
}
@property(nonatomic, weak) UILabel *addressLab;
@property(nonatomic, weak) UIImageView *RQView;
@property(nonatomic, strong) NSMutableArray *arrDS;
@property(nonatomic, weak) TDTitleBtn *rightbtn;
@property(nonatomic, strong) NSMutableArray *coinNameArray;
@end

@implementation TransferInController

- (NSMutableArray *)arrDS
{
    if (!_arrDS) {
        _arrDS = [NSMutableArray array];
    }
    return _arrDS;
}
-(NSMutableArray *)coinNameArray{
    
    if(!_coinNameArray){
        
        _coinNameArray=[NSMutableArray array];
    }
    return _coinNameArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"转入";
    [self setupUI];
    [self loadData];

    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.rightbtn setTitle:self.type?:@"--" forState:UIControlStateNormal];
//    [self loadData];
}

- (void)loadData
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.type forKey:@"coinname"];
    [dic setValue:[AccountTools shared].currentAccount.token forKey:@"token"];
    [dic setValue:[AccountTools shared].currentAccount.userId forKey:@"uid"];

    
    [[TRRequest request] Post:kSetURL(kTransferIn) parameters:dic success:^(TRRequest *request, NSDictionary *responseDict) {
        
        if ([[responseDict valueForKey:@"error"] integerValue] == 0) {
            
            [self setupAddress:[[responseDict valueForKey:@"data"] valueForKey:@"users_coin_address"]];
            self->zc_min=[[responseDict valueForKey:@"data"] valueForKey:@"zc_min"] ;
            NSString *str=[NSString stringWithFormat:@"1.请勿向上述地址充值任何非 %@ 资产。否则资产将不可找回。\n2.您充值至上述地址后，需要整个网络节点的确认后到账，请耐心等待。\n3.最小充值金额：%@ %@，小于最小金额的充值将不会上账且无法退回。\n4.通过管家云银包之间转账操作，支持瞬间到账。",self.type,self->zc_min,self.type];
            [self->textLab setText:str];
        }else
        {
            self.RQView.image = nil;
            self.addressLab.text = nil;
            [MBProgressHUD showErrorWithText:[responseDict valueForKey:@"error_msg"]];
        }
    } failure:^(TRRequest *request, NSError *error) {
        self.RQView.image = nil;
        self.addressLab.text = nil;
        
    }];
}


- (void)setupAddress:(NSString *)address{
    self.RQView.image = [WJTools createNonInterpolatedUIImageWithDataString:address withSize:self.RQView.yzWidth];
    self.addressLab.text = address;
}


- (void)setupUI{
    TDTitleBtn * rightbtn = [[TDTitleBtn alloc]initWithFrame:CGRectMake(kScreenWidth - 80 - 15, kNavHeight - 35, 80, 23)];
    rightbtn.layer.cornerRadius = 11;
    rightbtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    rightbtn.titleLabel.textAlignment = 1;
    rightbtn.titleLabel.font = kFONT(12);
    [rightbtn setTitle:self.type?:@"--" forState:UIControlStateNormal];
    [rightbtn setImage:kImageName(@"arrow_down") forState:UIControlStateNormal];
    [rightbtn setImage:kImageName(@"arrow_up") forState:UIControlStateSelected];
    rightbtn.layer.borderColor = [kTradeWhiteColor CGColor];
    rightbtn.layer.borderWidth = 0.5f;
    [rightbtn addTarget:self action:@selector(rightnBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.rightbtn = rightbtn;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightbtn];;
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    UIImageView *topImage = [UIImageView new];
    topImage.image = kImageName(@"image_bg_transfer01");
    [scrollView addSubview:topImage];
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(scrollView.mas_top).mas_offset(20);
        make.left.mas_equalTo(scrollView.mas_left).mas_offset(18);
        make.width.mas_equalTo(kScreenWidth - 36);
        make.height.mas_equalTo(((kScreenWidth - 36)*(565.0f/680.0f)));
    }];
    
    UIImageView *bootomImage = [UIImageView new];
    bootomImage.image = kImageName(@"image_bg_transfer02");
    
    [scrollView addSubview:bootomImage];
    
    [bootomImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(topImage.mas_bottom);
        make.left.mas_equalTo(scrollView.mas_left).offset(18);
        make.width.mas_equalTo(topImage.mas_width);
        make.height.mas_equalTo((kScreenWidth - 36)*(295.0f/680.0f));
    }];
    
    UIView *line = [UIView new];
    [scrollView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(bootomImage.mas_bottom).mas_offset(20);
    }];
    
    textLab = [[UILabel alloc]init];
    textLab.numberOfLines = 0;
    zc_min=@"0.00";
    NSString *str=[NSString stringWithFormat:@"1.请勿向上述地址充值任何非 %@ 资产。否则资产将不可找回。\n2.您充值至上述地址后，需要整个网络节点的确认后到账，请耐心等待。\n3.最小充值金额：%@ %@，小于最小金额的充值将不会上账且无法退回。\n4.通过管家云银包之间转账操作，支持瞬间到账。",self.type,zc_min,self.type];
    
//    textLab.text = @"请勿向上述地址充值任何非 BTC 资产。否则资产将不可找回。\n您充值至上述地址后，需要整个网络节点的确认后到账，请耐心等待。\n最小充值金额：0.01 BTC，小于最小金额的充值将不会上账切无法退回。\n通过管家云银包之间转账操作，支持瞬间到账。";
    textLab.text=str;
    textLab.font = kFONT(13);
    textLab.textColor = rgb(131,131,131);
    [scrollView addSubview:textLab];
    
    [textLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(36);
        make.width.mas_equalTo(kScreenWidth - 72);
        make.top.mas_equalTo(line.mas_bottom).mas_offset(12);
        //        make.height.mas_equalTo(100);
        make.bottom.equalTo(scrollView);
    }];
    
    
    
    titleLab = [UILabel labelWithFont:kFONT(14) color:[UIColor blackColor] text:[NSString stringWithFormat:@"%@转入地址",self.type?:@"--"]];
    saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.titleLabel.font = kFONT(14);
    [saveBtn setImage:kImageName(@"icon_add") forState:UIControlStateNormal];
    [saveBtn setTitle:@"保存到相册" forState:UIControlStateNormal];
    [saveBtn setTitleColor:rgb(75,74,74) forState:UIControlStateNormal];
    saveBtn.layer.cornerRadius = 13;
    saveBtn.layer.borderColor = rgb(186,186,186).CGColor;
    saveBtn.layer.borderWidth = 1;
    
    __weak typeof (UIButton *)weakSave = saveBtn;
    [[saveBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if (!self.RQView.image) {
            return ;
        }
        weakSave.userInteractionEnabled = NO;
        UIImageWriteToSavedPhotosAlbum(self->_RQView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
    }];
    UIImageView *RQView = [UIImageView new];
    self.RQView = RQView;
    
    [topImage addSubview:titleLab];
    [topImage addSubview:saveBtn];
    [topImage addSubview:RQView];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImage).offset(27);
        make.centerX.equalTo(topImage);
    }];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(104);
        make.height.mas_equalTo(26);
        make.bottom.equalTo(topImage).offset(-12);
        make.centerX.equalTo(topImage);
    }];
    
    [RQView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSave.mas_top).offset(-12);
        make.top.mas_equalTo(titleLab.mas_bottom).offset(12);
        make.centerX.equalTo(topImage);
        make.height.equalTo(RQView.mas_width); /// 约束长度等于宽度
        make.width.equalTo(RQView.mas_height);
    }];
    
    
    UILabel *address = [UILabel labelWithFrame:CGRectZero font:kFONT(14) color:rgb(75,74,74)];
    
    address.adjustsFontSizeToFitWidth = YES;
    address.numberOfLines = 0;
    address.textAlignment = NSTextAlignmentCenter;
    [bootomImage addSubview:address];
    self.addressLab = address;
    
    UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    copyBtn.titleLabel.font = kFONT(14);
    copyBtn.layer.cornerRadius = 13;
    copyBtn.layer.borderColor = rgb(186,186,186).CGColor;
    copyBtn.layer.borderWidth = 1;

    [copyBtn setImage:kImageName(@"icon_add") forState:UIControlStateNormal];
    [copyBtn setTitle:@"复制地址" forState:UIControlStateNormal];
    [copyBtn setTitleColor:rgb(75,74,74) forState:UIControlStateNormal];
    [bootomImage addSubview:copyBtn];
    
    topImage.userInteractionEnabled = YES;
    bootomImage.userInteractionEnabled = YES;

    [copyBtn addTarget:self action:@selector(copyAction) forControlEvents:UIControlEventTouchUpInside];
    [address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bootomImage).offset(25);
        make.left.equalTo(bootomImage).offset(25);
        make.right.equalTo(bootomImage).offset(-25);
        make.bottom.mas_equalTo(copyBtn.mas_top);

    }];
    
    [copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(104);
        make.height.mas_equalTo(26);
        make.bottom.equalTo(bootomImage).offset(-40);
        make.centerX.equalTo(bootomImage);
    }];
    
}


- (void)copyAction{
    if (![WJTools isBlankString:self.addressLab.text]) {
        [MBProgressHUD showSuccessWithText:@"复制成功"];
        //复制到剪贴板
        [UIPasteboard generalPasteboard].string =
        self.addressLab.text;
    }
    
}

- (void)btnClick:(UIButton*)btn
{
  
    MyLog(@"btnClick");
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (!error) {
        [MBProgressHUD showSuccessWithText:@"保存成功"];
    }else
    {
        [MBProgressHUD showErrorWithText:@"保存失败"];
    }
    saveBtn.userInteractionEnabled = YES;
    NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}
- (void)setType:(NSString *)type
{
    _type = type;
    titleLab.text = [NSString stringWithFormat:@"%@转入地址",self.type];
}
- (void)rightnBtnClick
{
    ChoseCoinController *vc = [ChoseCoinController new];
    
    vc.coinClickBlock = ^(CoinListModel *model) {
        self.type = model.name;
        [self.rightbtn setTitle:model.name forState:UIControlStateNormal];
        [self loadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
@end
